const https = require('https');
const http = require('http');
const requestPromise = require('request-promise');
const sync_Request = require('sync-request');

function download(url, callback) {
  https.get(url, function (res) {
    var data = "";
    res.on('data', function (chunk) {
      data += chunk;
    });
    res.on('end', function () {
      callback(data);
    });
  }).on('error', function () {
    callback(null);
  })
}

function download2(url, callback) {
  http.get(url, function (res) {
    var data = "";
    res.on('data', function (chunk) {
      data += chunk;
    });
    res.on('end', function () {
      callback(data);
    });
  }).on('error', function () {
    callback(null);
  })
}

function syncRequest(method, url) {
  let res = sync_Request(method, url);
  if (res.statusCode === 200) {
    return res.getBody('utf8');
  } else {
    return null;
  }
}

exports.download = download;
exports.download2 = download2;
exports.syncRequest = syncRequest;